package com.carl.zeus.web.system;

import com.baomidou.mybatisplus.plugins.Page;
import com.carl.zeus.common.annotation.Log;
import com.carl.zeus.common.base.BaseController;
import com.carl.zeus.common.constant.BusinessCode;
import com.carl.zeus.common.constant.BusinessMsg;
import com.carl.zeus.common.util.BusinessMsgUtil;
import com.carl.zeus.common.util.R;
import com.carl.zeus.model.system.SysMenu;
import com.carl.zeus.model.system.SysRole;
import com.carl.zeus.model.vo.MenuVO;
import com.carl.zeus.model.vo.Tree;
import com.carl.zeus.server.system.SysMenuService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * 
 * @author 作者: zcm
 * @date 创建时间：2017年12月8日 下午1:32:02
 */
@Controller
@RequestMapping(value="sys/menu")
public class MenuController extends BaseController {
	
	@Autowired
	private SysMenuService sysMenuService;

	@RequestMapping(value = "nav", method = RequestMethod.GET)
	@ResponseBody
	public List<SysMenu> left() {
		List<SysMenu> menuList = sysMenuService.getUserMenuList(getUserId());
		return menuList;
	}

	/**
	 * 跳转到菜单列表
	 * @return
	 */
	@Log("菜单列表")
	@RequestMapping(value = "list", method = RequestMethod.GET)
	public String list() {
		return "system/menu";
	}

	@RequestMapping(value = "allList", method = RequestMethod.GET)
	@ResponseBody
	public List<SysMenu> findAll() {
		List<SysMenu> menuList = sysMenuService.queryList(new HashMap<>());
		return menuList;
	}

	/**
	 * 菜单信息
	 */
	@Log("菜单信息")
	@RequestMapping("/info/{id}")
	@ResponseBody
	public SysMenu info(@PathVariable("id") Long menuId){
		SysMenu menu = sysMenuService.selectById(menuId);
		return menu;
	}

	/**
	 * 图标页面
	 * @return
	 */
	@Log("菜单图标")
	@RequestMapping(value = "icon", method = RequestMethod.GET)
	public String icon() {
		return "system/menu-icon";
	}

	/**
	 * 添加菜单
	 * @return
	 */
	@Log("新增菜单")
	@RequestMapping(value = "add", method = RequestMethod.GET)
	public String add(Model model) {
		model.addAttribute("menu", new SysMenu());
		return "system/menu-edit";
	}

	/**
	 *
	 * @param id
	 * @return
	 */
	@Log("编辑菜单")
	@RequestMapping(value = "edit/{id}", method = RequestMethod.GET)
	public String edit(@PathVariable("id") Long id, Model model) {
		SysMenu menu = sysMenuService.selectById(id);
		if (menu.getParentId() != null && menu.getParentId() != 0) {
			menu.setParentName(sysMenuService.selectById(menu.getParentId()).getName());
		}
		model.addAttribute("menu", menu);
		return "system/menu-edit";
	}

	@Log("删除菜单")
	@RequestMapping(value = "delete/{id}", method = RequestMethod.POST)
	@RequiresPermissions("sys:menu:delete")
	@ResponseBody
	public R delete(@PathVariable("id") Long id, Model model) {
		//判断是否有子菜单或按钮
		List<SysMenu> menuList = sysMenuService.queryListParentId(id);
		if(menuList.size() > 0){
			return R.error("请先删除子菜单或按钮");
		}
		sysMenuService.delete(id);
		return R.ok();
	}

	/**
	 * 分页--列表
	 * @param page
	 * @param limit
	 * @return
	 */
	@RequestMapping(value = "listAjax", method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public List<SysMenu> listAjax(@RequestParam(value = "page", defaultValue = "1") int page,
						   @RequestParam(value = "limit", defaultValue = "100") int limit,
						   @RequestParam(value = "key", defaultValue = "") String key,
						   @RequestParam(value = "value", defaultValue = "") String value) {
		Map<String, Object> map = new HashMap<>();
		//List<SysMenu> menuList = sysMenuService.listAjax(page, limit, key, value);
		Page<SysMenu> pagination = new Page<>();
		pagination.setSize(limit);
		pagination.setCurrent(page);
		Page<SysMenu> menuPage = sysMenuService.selectPage(pagination, key,value);

		for (int i = 0; i < menuPage.getRecords().size(); i++) {
			SysMenu parentMenuEntity = sysMenuService.selectById(menuPage.getRecords().get(i).getParentId());
			if(parentMenuEntity != null){
				menuPage.getRecords().get(i).setParentName(parentMenuEntity.getName());
			}
			menuPage.getRecords().get(i).setChildren(sysMenuService.queryListParentId(menuPage.getRecords().get(i).getId()));
		}

		map.put("code", 0);
		map.put("msg", "");
		map.put("count", menuPage.getTotal());
		map.put("data", menuPage.getRecords());
		return menuPage.getRecords();
	}

	/**
	 * 选择父菜单页面
	 * @param model
	 * @return
	 */
	@Log("选择父级菜单")
	@RequestMapping(value = "/parentMenu", method = RequestMethod.GET)
	public String roleGrantPage(Model model){
//		SysRole role = sysRoleService.selectRoleMenuByRoleId(id);
//		model.addAttribute("menus", menus);
		return "system/menu-parent";
	}

	/**
	 * 获取当前用户所属菜单资源Tree菜单展示
	 */
	@RequestMapping(value = "parentMenuAjax", method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public List<MenuVO> ajaxMenuTreeList(){
		return sysMenuService.selectParentMenuAllTree();
	}

	/**
	 * 保存菜单信息
	 * @return
	 */
	@Log("保存菜单")
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@ResponseBody
	public BusinessMsg ajaxSaveMenu(SysMenu menu){
		if (StringUtils.isNotEmpty(menu.getName()) && menu.getCreateTime() == null) {
			// 创建角色
			menu.setCreateTime(new Date());
			sysMenuService.saveOrUpdateMenu(menu);
		}
		return BusinessMsgUtil.returnCodeMessage(BusinessCode.GLOBAL_SUCCESS);
	}
}
